<?php

namespace app\api\controllers;

use app\api\Constant;
use app\models\GkyQyhDepartment;
use yii\web\Controller;
use app\api\Response;
use app\models\GkyUser;
use yii\helpers\ArrayHelper;
use yii\db\Query;
class DeptController extends Controller {
    
    /**
     * 获取所有部门
     * @return type
     */
    public function actionListdept(){
        $depts = GkyQyhDepartment::find()->all();
         Response::setHeader();
        return json_encode(ArrayHelper::toArray($depts));
    }


    /**
     * 获取部门与部门下的人员
     * @return type
     */
    public function actionList() {
        $dept = GkyQyhDepartment::findMembers(Constant::$GKY_CORPID);
        Response::setHeader();
        return json_encode(ArrayHelper::toArray($dept));
    }
    /**
     * 搜索功能
     * @param type $username
     */
     public function actionSearch($name){
         $users= \app\models\GkyQyhUser::find()
              ->where(['like','name',$name])
              ->all();
         $usersArr=ArrayHelper::toArray($users);
         $result=array();
         foreach($usersArr as $userArr){
             $dept=(new Query())
                     ->select('name as dept')
                     ->from('gky_qyh_department as d')
                     ->leftJoin('gky_qyh_dept_user as du','d.id=du.dept_id')
                     ->where(['user_id'=>$userArr['userid']])
                     ->one();
//             $userArr[]=['dept'=>$dept['dept']];
             $userArr=  array_merge($userArr,$dept);
             $result[]=$userArr;
         }
//         var_dump($result);die();
         
         
          Response::setHeader();
        echo json_encode($result);
    }
}
